package com.cloud.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String message) {
//        System.out.println("消费者接收到了消息：【" + message + "】");
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue1(String message) throws InterruptedException {
        System.out.println("消费者 1 接收到了消息：【" + message + "】");
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue2(String message) throws InterruptedException {
        System.out.println("消费者 2 接收到了消息：【" + message + "】");
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("消费者接收到了fanout.queue1消息：【" + message + "】");
        // Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("消费者接收到了fanout.queue2消息：【" + message + "】");
        // Thread.sleep(200);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"), // 绑定队列
            exchange = @Exchange(name = "exchange1.direct"), // 绑定交换机，type属性默认为direct
            key = {"red", "blue"} // 指定key
    ))
    public void listenDirectQueue1(String message) {
        System.out.println("消费者接收到了direct.queue1消息：【" + message + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"), // 绑定队列
            exchange = @Exchange(name = "exchange1.direct"), // 绑定交换机，type属性默认为direct
            key = {"red", "yellow"} // 指定key
    ))
    public void listenDirectQueue2(String message) {
        System.out.println("消费者接收到了direct.queue2消息：【" + message + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(value = "exchange1.topic", type = ExchangeTypes.TOPIC),
            key = "china.#" // #是通配符
    ))
    public void listenTopicQueue1(String message) {
        System.out.println("消费者接收到了topic.queue1消息：【" + message + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(value = "exchange1.topic", type = ExchangeTypes.TOPIC),
            key = "#.weather" // #是通配符
    ))
    public void listenTopicQueue2(String message) {
        System.out.println("消费者接收到了topic.queue2消息：【" + message + "】");
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> message) {
        System.out.println("接收到了object.queue的消息：----  " + message);
    }
}
